<template>
  <div class="texture-raindrop">
    <div id="scene"></div>
  </div>
</template>

<script>
import * as THREE from "three";
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";

let scene;
let camera;
let renderer;
let controls;
export default {
  name: "demo",
  data() {
    return {
      publicPath: process.env.BASE_URL,
      properties: {
        size: {
          name: "size",
          value: 4,
          min: 0,
          max: 20,
          step: 0.1,
        },
        opacity: {
          name: "opacity",
          value: 0.6,
          min: 0,
          max: 1,
          step: 0.1,
        },
        transparent: true,
        sizeAttenuation: true,
        color: "#ccffcc",
      },
      points: null,
    };
  },
  mounted() {
    this.init();
  },
  methods: {
    init() {
      this.createScene();
      this.createLight();
      this.createCamera();
      this.createRender();
      this.createControls();
      this.render();
    },
    createScene() {
      scene = new THREE.Scene();
    },
    createParticleSystem() {
      const textureLoader = new THREE.TextureLoader();
      const rainTexture = textureLoader.load();
    },
    // 创建光源
    createLight() {
      const ambientLight = new THREE.AmbientLight(0xffffff, 0.1); // 创建环境光
      scene.add(ambientLight); // 将环境光添加到场景

      const spotLight = new THREE.PointLight(0xffffff); // 创建聚光灯
      spotLight.position.set(0, 10, 20);
      spotLight.castShadow = true;
      scene.add(spotLight);
    },
    // 创建相机
    createCamera() {
      const element = document.getElementById("scene");
      const k = element.clientWidth / element.clientHeight; // 窗口宽高比
      camera = new THREE.PerspectiveCamera(35, k, 0.1, 1000);
      camera.position.set(0, 0, 80); // 设置相机位置

      scene.add(camera);
    },
    // 创建渲染器
    createRender() {
      const element = document.getElementById("scene");
      renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true });
      renderer.setSize(element.clientWidth, element.clientHeight); // 设置渲染区域尺寸
      renderer.shadowMap.enabled = true; // 显示阴影
      renderer.shadowMap.type = THREE.PCFSoftShadowMap;
      renderer.setClearColor(0x3f3f3f, 1); // 设置背景颜色
      element.appendChild(renderer.domElement);
    },
    // 创建控件对象
    createControls() {
      controls = new OrbitControls(camera, renderer.domElement);
    },
    render() {
      renderer.render(scene, camera);
      requestAnimationFrame(this.render);
    },
  },
};
</script> 

<style scoped>
.texture-raindrop {
  width: 100%;
  height: 100%;
  position: relative;
}
#scene {
  width: 100%;
  height: 100%;
}
</style>
